<?php
/**
 * User.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-14 01:48:43
 * @modified    2023-02-14 01:48:43
 *
 */

namespace App\Models\Customer;

use App\Models\BaseModel;
use App\Models\Customer;
use App\Repositories\Customer\UserRepo;

class User extends BaseModel
{
    protected $table = 'customer_user';

    protected $fillable = [
        'name',
        'gender',
        'phone',
        'office_phone',
        'email',
        'birthday',
        'duty',
        'project_role',
        'customer_id',
        'customer_name',
        'country_id',
        'country_name',
        'province_id',
        'province_name',
        'city_id',
        'city_name',
        'county_id',
        'county_name',
        'address',
        'status',
        'create_user_id',
    ];

    protected $appends = ['is_default', 'detail_address', 'gender_text'];

    public function scopeByPhone($query, $phone)
    {
        return $query->where('phone', $phone);
    }

    public function scopeByName($query, $name)
    {
        return $query->where('name', $name);
    }

    public function scopeByEmail($query, $email)
    {
        return $query->where('email', $email);
    }

    public function scopeByCustomerId($query, $customerId)
    {
        return $query->where('customer_id', $customerId);
    }

    public function scopeWithCustomerTag($query, $tagName)
    {
        $query->join('customer', 'customer.id', '=', 'customer_user.customer_id')
            ->where('customer.tag_name', 'like', "%{$tagName}%");
    }

    public function scopeWithCustomerSource($query, $source)
    {
        $query->join('customer', 'customer.id', '=', 'customer_user.customer_id')
            ->where('customer.source', 'like', "%{$source}%");
    }

    public function scopeWithCustomerSafeLevel($query, $safeLevel)
    {
        $query->join('customer', 'customer.id', '=', 'customer_user.customer_id')
            ->where('customer.safe_level', 'like', "%{$safeLevel}%");
    }

    public function scopeWithCustomerOwnerName($query, $ownerName)
    {
        $query->join('customer', 'customer_id', '=', 'customer_user.customer_id')
            ->where('customer.owner_name', 'like', "%{$ownerName}%");
    }

    public function getIsDefaultAttribute()
    {
        $customerUserId = $this->customer->customer_user_id ?? 0;
        return $this->id == $customerUserId;
    }

    public function getDetailAddressAttribute()
    {
        return $this->province_name . $this->city_name . $this->county_name . $this->address;
    }

    public function getGenderTextAttribute()
    {
        $gender = $this->gender;
        if (in_array($gender, ['男', '女', '未知'])) {
            return $gender;
        }
        $params = collect(UserRepo::params()['gender'])->keyBy('code')->toArray();
        $keys = array_keys($params);
        if (!in_array($gender, $keys)) {
            return UserRepo::GENDER_SECRET_TITLE;
        }
        return $params[$gender]['title'];
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }
}